package com.middlegroup.common.wrapper;

import java.util.Enumeration;
import java.util.Map;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * @author pc
 */
public class ParameterRequestWrapper extends HttpServletRequestWrapper {
    private Map params;

    public ParameterRequestWrapper(HttpServletRequest request, Map newParams) {
        super(request);
        this.params = newParams;
    }

    @Override
    public Map getParameterMap() {
        return this.params;
    }

    @Override
    public Enumeration getParameterNames() {
        Vector l = new Vector(this.params.keySet());
        return l.elements();
    }

    @Override
    public String[] getParameterValues(String name) {
        Object v = this.params.get(name);
        return v == null?null:(v instanceof String[]?(String[])((String[])v):(v instanceof String?new String[]{(String)v}:new String[]{v.toString()}));
    }

    @Override
    public String getParameter(String name) {
        Object v = this.params.get(name);
        if(v == null) {
            return null;
        } else if(v instanceof String[]) {
            String[] strArr = (String[])((String[])v);
            return strArr.length > 0?strArr[0]:null;
        } else {
            return v instanceof String?(String)v:v.toString();
        }
    }
}
